﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using BIS.Portal.Core.Base;
using BIS.Portal.Core.Global;
using BIS.Portal.Modules.CMS.BO;
using BIS.Portal.Modules.CMS.Entity;
using BIS.Utility.Converter;


namespace BIS.Portal.Modules.CMS
{
    public partial class CmsMediaDetail : ControlBase
    {
        #region Load data setting
        public bool HienThiMediaAnh
        {
            get { return GetControlSetting<bool>(ModuleSettingNames.CmsMediaDetail.ddlMediaAnh, PortalContext.LanguageAlias); }
        }
        public string SLMediaAnhHienThi
        {
            get { return GetControlSetting<string>(ModuleSettingNames.CmsMediaDetail.txtSLMediaAnh, PortalContext.LanguageAlias); }
        }
        public bool HienThiTenAnh
        {
            get { return GetControlSetting<bool>(ModuleSettingNames.CmsMediaDetail.ddlTenAnh, PortalContext.LanguageAlias); }
        }
        public bool HienThiMediaVideo
        {
            get { return GetControlSetting<bool>(ModuleSettingNames.CmsMediaDetail.ddlMediaVideo, PortalContext.LanguageAlias); }
        }
        public string SLMediaVideoHienThi
        {
            get { return GetControlSetting<string>(ModuleSettingNames.CmsMediaDetail.txtMediaVideo, PortalContext.LanguageAlias); }
        }
        public bool HienThiTenAnhVideo
        {
            get { return GetControlSetting<bool>(ModuleSettingNames.CmsMediaDetail.ddlTenAnhVD, PortalContext.LanguageAlias); }
        }
        public bool HienThiMediaFlash
        {
            get { return GetControlSetting<bool>(ModuleSettingNames.CmsMediaDetail.ddlMediaFlash, PortalContext.LanguageAlias); }
        }
        public string SLMediaFlashHienThi
        {
            get { return GetControlSetting<string>(ModuleSettingNames.CmsMediaDetail.txtMediaFlash, PortalContext.LanguageAlias); }
        }
        public bool HienThiTenAnhFlash
        {
            get { return GetControlSetting<bool>(ModuleSettingNames.CmsMediaDetail.ddlTenAnhFlash, PortalContext.LanguageAlias); }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            LoadData();
        }
        private void LoadData()
        {
            int countMediaAnh = 0;
            int countMediaVideo = 0;
            int countMediaFlash = 0;
            int slAnh = Functions.GetInt(SLMediaAnhHienThi);
            int slVideo = Functions.GetInt(SLMediaVideoHienThi);
            int slFlash = Functions.GetInt(SLMediaFlashHienThi);

            #region Get data query string
            int page_index_img = 1;
            int page_index_video = 1;
            int page_index_file = 1;
            int aticle_id = 0;
            if (Request[ModuleQueryStringKey.image_page_index] != null)
            {
                page_index_img = Functions.GetInt(Request[ModuleQueryStringKey.image_page_index]);
            }
            if (Request[ModuleQueryStringKey.video_page_index] != null)
            {
                page_index_video = Functions.GetInt(Request[ModuleQueryStringKey.video_page_index]);
            }
            if (Request[ModuleQueryStringKey.file_page_index] != null)
            {
                page_index_file = Functions.GetInt(Request[ModuleQueryStringKey.file_page_index]);
            }
            if (Request[ModuleQueryStringKey.ArticleId] != null)
            {
                aticle_id = Functions.GetInt(Request[ModuleQueryStringKey.ArticleId]);
            }
            #endregion

            #region Bind Data Media Anh
            if (HienThiMediaAnh)
            {
                IList<ClientCmsMediaEnity> list = ClientCmsArticleBO.GetCmsMediaDetailByArticleId(aticle_id, PortalContext.LanguageInUseId, 3, page_index_img-1, slAnh, ref countMediaAnh);
                Media_Anh.DataSource = list;
                Media_Anh.DataBind();
            }
            if (slAnh > 0)
            {
                this.PagingControlAnh.QueryStringName = ModuleQueryStringKey.image_page_index;
                this.PagingControlAnh.UrlRewriteRule = ModuleQueryStringKey.UrlMediaDetailWithImagePaging;
                this.PagingControlAnh.TotalRecord = countMediaAnh;
                this.PagingControlAnh.RecordPerPage = slAnh;
                this.PagingControlAnh.RefreshPaging(new string[] { ModuleQueryStringKey.video_page_index, ModuleQueryStringKey.file_page_index }, new string[] { Functions.GetString(page_index_video), Functions.GetString(page_index_file) });
            }

            #endregion

            #region Bind Data media Video
            if (HienThiMediaVideo)
            {
                IList<ClientCmsMediaEnity> listVD = ClientCmsArticleBO.GetCmsMediaDetailByArticleId(aticle_id, PortalContext.LanguageInUseId, 2, page_index_video - 1, slVideo, ref countMediaVideo);

                Media_Video.DataSource = listVD;
                Media_Video.DataBind();
            }
            if (slVideo > 0)
            {
                this.PagingControlVideo.QueryStringName = ModuleQueryStringKey.video_page_index;
                this.PagingControlVideo.UrlRewriteRule = ModuleQueryStringKey.UrlMediaDetailWithImagePaging;
                this.PagingControlVideo.TotalRecord = countMediaVideo;
                this.PagingControlVideo.RecordPerPage = slVideo;
                this.PagingControlVideo.RefreshPaging(new string[] { ModuleQueryStringKey.image_page_index, ModuleQueryStringKey.file_page_index }, new string[] { Functions.GetString(page_index_img), Functions.GetString(page_index_file)});
            }
            #endregion

            #region Bind Data media Flash
            if (HienThiMediaFlash)
            {
                IList<ClientCmsMediaEnity> listFlash = ClientCmsArticleBO.GetCmsMediaDetailByArticleId(aticle_id, PortalContext.LanguageInUseId, 1, page_index_file - 1, slFlash, ref countMediaFlash);

                Media_File.DataSource = listFlash;
                Media_File.DataBind();
            }
            if (slFlash > 0)
            {
                this.PagingControlFile.QueryStringName = ModuleQueryStringKey.file_page_index;
                this.PagingControlFile.UrlRewriteRule = ModuleQueryStringKey.UrlMediaDetailWithImagePaging;
                this.PagingControlFile.TotalRecord = countMediaFlash;
                this.PagingControlFile.RecordPerPage = slFlash;
                this.PagingControlFile.RefreshPaging(new string[] { ModuleQueryStringKey.image_page_index, ModuleQueryStringKey.video_page_index }, new string[] { Functions.GetString(page_index_img), Functions.GetString(page_index_video) });
            }
            #endregion


        }

        protected void Media_Anh_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                ClientCmsMediaEnity objMedia = (ClientCmsMediaEnity)e.Item.DataItem;
                if (null != objMedia)
                {
                    #region Bind data Link ảnh
                    HyperLink LinkFullImg = e.Item.FindControl("LinkFullImg") as HyperLink;
                    if (LinkFullImg != null)
                    {
                        LinkFullImg.ImageUrl = objMedia.MediaThumbnails;
                        LinkFullImg.NavigateUrl = objMedia.MediaSrc;
                        LinkFullImg.ToolTip = objMedia.MediaTitle;
                    }
                    Image imgAnh = e.Item.FindControl("Anh_Minh_Hoa") as Image;
                    if (imgAnh != null)
                    {
                        imgAnh.ImageUrl = objMedia.MediaThumbnails;
                        imgAnh.AlternateText = objMedia.MediaTitle;
                    }
                    #endregion

                    #region Bind data hiển thị tên ảnh
                    if (HienThiTenAnh)
                    {
                        HyperLink linkTenAnh = e.Item.FindControl("Ten_Anh") as HyperLink;
                        if (linkTenAnh != null)
                        {
                            if (objMedia.MediaLink != "")
                            {
                                linkTenAnh.NavigateUrl = objMedia.MediaLink;
                                linkTenAnh.Text = objMedia.MediaTitle;
                                linkTenAnh.Target = "_blank";
                            }
                            else
                            {
                                linkTenAnh.NavigateUrl = objMedia.MediaSrc;
                                linkTenAnh.Text = objMedia.MediaTitle;
                                linkTenAnh.Attributes.Add("rel", "prettyPhoto");
                            }
                        }
                    }
                    #endregion


                }
            }
        }
        protected void Media_Video_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                ClientCmsMediaEnity objMedia = (ClientCmsMediaEnity)e.Item.DataItem;
                if (null != objMedia)
                {
                    #region Bind data link Video
                    HyperLink LinkFullVD = e.Item.FindControl("LinkFullVD") as HyperLink;
                    if (LinkFullVD != null)
                    {
                        LinkFullVD.ImageUrl = objMedia.MediaThumbnails;
                        LinkFullVD.NavigateUrl = objMedia.MediaSrc;
                        LinkFullVD.ToolTip = objMedia.MediaTitle;

                    }
                    Image imgAnh = e.Item.FindControl("Anh_Minh_Hoa_VD") as Image;
                    if (imgAnh != null)
                    {
                        imgAnh.ImageUrl = objMedia.MediaThumbnails;
                        imgAnh.AlternateText = objMedia.MediaTitle;
                    }

                    #endregion

                    #region Bind data Tên Video
                    if (HienThiTenAnhVideo)
                    {
                        HyperLink linkTenAnh = e.Item.FindControl("Ten_Anh_VD") as HyperLink;
                        if (linkTenAnh != null)
                        {
                            if (objMedia.MediaLink != "")
                            {
                                linkTenAnh.NavigateUrl = objMedia.MediaLink;
                                linkTenAnh.Text = objMedia.MediaTitle;
                                linkTenAnh.Target = "_blank";
                            }
                            else
                            {
                                linkTenAnh.NavigateUrl = objMedia.MediaSrc;
                                linkTenAnh.Text = objMedia.MediaTitle;
                                linkTenAnh.Attributes.Add("rel", "prettyPhoto");
                            }
                        }
                    }
                    #endregion

                }
            }
        }
        protected void Media_File_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                ClientCmsMediaEnity objMedia = (ClientCmsMediaEnity)e.Item.DataItem;
                if (null != objMedia)
                {
                    #region Bind data link Video
                    HyperLink LinkFullFlash = e.Item.FindControl("LinkFullFile") as HyperLink;
                    if (LinkFullFlash != null)
                    {
                        LinkFullFlash.ImageUrl = objMedia.MediaThumbnails;
                        LinkFullFlash.NavigateUrl = objMedia.MediaSrc;
                        LinkFullFlash.ToolTip = objMedia.MediaTitle;

                    }
                    Image imgAnh = e.Item.FindControl("Anh_Minh_Hoa_File") as Image;
                    if (imgAnh != null)
                    {
                        imgAnh.ImageUrl = objMedia.MediaThumbnails;
                        imgAnh.AlternateText = objMedia.MediaTitle;
                    }

                    #endregion

                    #region Bind data Tên Video
                    if (HienThiTenAnhVideo)
                    {
                        HyperLink linkTenAnh = e.Item.FindControl("Ten_Anh_File") as HyperLink;
                        if (linkTenAnh != null)
                        {
                            if (objMedia.MediaLink != "")
                            {
                                linkTenAnh.NavigateUrl = objMedia.MediaLink;
                                linkTenAnh.Text = objMedia.MediaTitle;
                                linkTenAnh.Target = "_blank";
                            }
                            else
                            {
                                linkTenAnh.NavigateUrl = objMedia.MediaSrc;
                                linkTenAnh.Text = objMedia.MediaTitle;
                                linkTenAnh.Attributes.Add("rel", "prettyPhoto");
                            }
                        }
                    }
                    #endregion

                }
            }
        }

    }
}